# Replication code for Taylor C. Boas and Amy Erica Smith, “Looks Like Me, Thinks Like Me: Descriptive Representation and Opinion Congruence in Brazil.” Latin American Research Review 54, 2 (2019).

# Analysis conducted in R 3.6.0 on MacOS 10.13.6

# NOTE: This file replicates Appendix Figure 5. We recommend running R replication files in the following order; please see readme.txt for details.
# 	1_merge_lapop.R
# 	2_merge_latinobarometro.R
# 	3_recode_reshape.R
# 	4_difference_in_distributions.R
# 	5_regressions.R
# 	6_civil_society_meeting.R
# 	7_mass_descriptives.R
# 	8_elite_descriptives.R
# 	9_mean_differences.R
#	10_difference_in_distributions_ks.R
#	11_elite_sample_simulation.R
#	12_converts_vs_lifelong.R

# Set working directory as appropriate
# setwd('~/Dropbox/brazil_leg_surveys/replication/')

# Clean desktop and load packages. Please make sure all necessary packages are installed.

library(Hmisc)

rm(list=ls(all=T))

load('elite.data.RData')
load('mass.data.RData')

# =====================================================
# Appendix Figure 5: Opinion Congruence: Differences in
# Distributions using KS Statistics
# =====================================================

# Functions for calculating differences in CDFs

wtd.ecdf<-function(x,weights=NULL) function(y) {
	cdf<-wtd.Ecdf(x,weights=weights,normwt=T,type='i/n')
	cdf<-data.frame(x=cdf$x,Fx=cdf$ecdf)
	cdf$x[1]<- -Inf
	sapply(y,function(z) cdf$Fx[max(which(round(cdf$x,5) <= round(z,5)))]) # Dealing with differences in precision that were causing equal numbers to not be recognized as such
}

ks<-function(x,y){
	vals<-sort(unique(c(x,y)))
	Fy<-ecdf(y)(vals)
	Fx<-ecdf(x)(vals)
	max(abs(Fy-Fx))
	}

wtd.ks<-function(x,y,weights.x,weights.y){
	vals<-sort(unique(c(x,y)))
	Fy<-wtd.ecdf(y,weights=weights.y)(vals)
	Fx<-wtd.ecdf(x,weights=weights.x)(vals)
	max(abs(Fy-Fx))
}

mass.data$mass_survey<-gsub('^([a-z]*)\\-{0,1}[0-9].*','\\1', mass.data$mass_id)

# Weights for economic regime preference, ideology. For masses, weighting each paired BLS wave equally. For elites, using proportions of each demographic category in each actual legislature.

mass.weight.econ.all<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)])))
mass.weight.econ.evang<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_evang)])))
mass.weight.econ.female<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_female)])))
mass.weight.econ.black<-data.frame((1/2)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_black)])))
mass.weight.econ.loweduc<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_loweduc)])))
mass.weight.econ.pt<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_party=='PT')])))
mass.weight.econ.psdb<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_party=='PSDB')])))
mass.weight.econ.pmdb<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_party=='PMDB')])))
mass.weight.econ.sp<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_uf=='SP')])))
mass.weight.econ.mg<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_econ)&(mass.data$mass_uf=='MG')])))
mass.weight.ideol.all<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)])))
mass.weight.ideol.evang<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_evang)])))
mass.weight.ideol.female<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_female)])))
mass.weight.ideol.black<-data.frame((1/2)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_black)])))
mass.weight.ideol.loweduc<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_loweduc)])))
mass.weight.ideol.pt<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_party=='PT')])))
mass.weight.ideol.psdb<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_party=='PSDB')])))
mass.weight.ideol.pmdb<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_party=='PMDB')])))
mass.weight.ideol.sp<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_uf=='SP')])))
mass.weight.ideol.mg<-data.frame((1/3)/prop.table(table(mass.data$mass_blsyear[!is.na(mass.data$mass_ideol)&(mass.data$mass_uf=='MG')])))

mass.data$weight.econ.all<-ifelse(is.na(mass.data$mass_econ),NA,mass.weight.econ.all[match(mass.data$mass_blsyear, mass.weight.econ.all[,1]),2])
mass.data$weight.econ.evang<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_evang,NA,mass.weight.econ.evang[match(mass.data$mass_blsyear, mass.weight.econ.evang[,1]),2])
mass.data$weight.econ.female<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_female,NA,mass.weight.econ.female[match(mass.data$mass_blsyear, mass.weight.econ.female[,1]),2])
mass.data$weight.econ.black<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_black,NA,mass.weight.econ.black[match(mass.data$mass_blsyear, mass.weight.econ.black[,1]),2])
mass.data$weight.econ.loweduc<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_loweduc,NA,mass.weight.econ.loweduc[match(mass.data$mass_blsyear, mass.weight.econ.loweduc[,1]),2])
mass.data$weight.econ.pt<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_party=='PT',NA,mass.weight.econ.pt[match(mass.data$mass_blsyear, mass.weight.econ.pt[,1]),2])
mass.data$weight.econ.psdb<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_party=='PSDB',NA,mass.weight.econ.psdb[match(mass.data$mass_blsyear, mass.weight.econ.psdb[,1]),2])
mass.data$weight.econ.pmdb<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_party=='PMDB',NA,mass.weight.econ.pmdb[match(mass.data$mass_blsyear, mass.weight.econ.pmdb[,1]),2])
mass.data$weight.econ.sp<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_uf=='SP',NA,mass.weight.econ.sp[match(mass.data$mass_blsyear, mass.weight.econ.sp[,1]),2])
mass.data$weight.econ.mg<-ifelse(is.na(mass.data$mass_econ)|!mass.data$mass_uf=='MG',NA,mass.weight.econ.mg[match(mass.data$mass_blsyear, mass.weight.econ.mg[,1]),2])
mass.data$weight.ideol.all<-ifelse(is.na(mass.data$mass_ideol),NA,mass.weight.ideol.all[match(mass.data$mass_blsyear, mass.weight.ideol.all[,1]),2])
mass.data$weight.ideol.evang<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_evang,NA,mass.weight.ideol.evang[match(mass.data$mass_blsyear, mass.weight.ideol.evang[,1]),2])
mass.data$weight.ideol.female<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_female,NA,mass.weight.ideol.female[match(mass.data$mass_blsyear, mass.weight.ideol.female[,1]),2])
mass.data$weight.ideol.black<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_black,NA,mass.weight.ideol.black[match(mass.data$mass_blsyear, mass.weight.ideol.black[,1]),2])
mass.data$weight.ideol.loweduc<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_loweduc,NA,mass.weight.ideol.loweduc[match(mass.data$mass_blsyear, mass.weight.ideol.loweduc[,1]),2])
mass.data$weight.ideol.pt<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_party=='PT',NA,mass.weight.ideol.pt[match(mass.data$mass_blsyear, mass.weight.ideol.pt[,1]),2])
mass.data$weight.ideol.psdb<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_party=='PSDB',NA,mass.weight.ideol.psdb[match(mass.data$mass_blsyear, mass.weight.ideol.psdb[,1]),2])
mass.data$weight.ideol.pmdb<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_party=='PMDB',NA,mass.weight.ideol.pmdb[match(mass.data$mass_blsyear, mass.weight.ideol.pmdb[,1]),2])
mass.data$weight.ideol.sp<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_uf=='SP',NA,mass.weight.ideol.sp[match(mass.data$mass_blsyear, mass.weight.ideol.sp[,1]),2])
mass.data$weight.ideol.mg<-ifelse(is.na(mass.data$mass_ideol)|!mass.data$mass_uf=='MG',NA,mass.weight.ideol.mg[match(mass.data$mass_blsyear, mass.weight.ideol.mg[,1]),2])

elite.weight.econ.all<-data.frame((1/3)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)])))
elite.weight.econ.evang<-data.frame(c(.4,.2,.4)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_evang)])))
elite.weight.econ.female<-data.frame(c(.31,.345,.345)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_female)])))
elite.weight.econ.black<-data.frame((1/2)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_black)])))
elite.weight.econ.loweduc<-data.frame(c(.375,.3,.325)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_loweduc)])))
elite.weight.econ.pt<-data.frame(c(.34,.32,.34)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_partythen=='PT')])))
elite.weight.econ.psdb<-data.frame(c(.37,.34,.29)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_partythen=='PSDB')])))
elite.weight.econ.pmdb<-data.frame(c(.325,.35,.325)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_partythen=='PMDB')])))
elite.weight.econ.sp<-data.frame((1/3)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_uf=='SP')])))
elite.weight.econ.mg<-data.frame((1/3)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_econ)&(elite.data$elite_uf=='MG')])))
elite.weight.ideol.all<-data.frame((1/3)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)])))
elite.weight.ideol.evang<-data.frame(c(.4,.2,.4)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_evang)])))
elite.weight.ideol.female<-data.frame(c(.31,.345,.345)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_female)])))
elite.weight.ideol.black<-data.frame((1/2)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_black)])))
elite.weight.ideol.loweduc<-data.frame(c(.375,.3,.325)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_loweduc)])))
elite.weight.ideol.pt<-data.frame(c(.34,.32,.34)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_partythen=='PT')])))
elite.weight.ideol.psdb<-data.frame(c(.37,.34,.29)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_partythen=='PSDB')])))
elite.weight.ideol.pmdb<-data.frame(c(.325,.35,.325)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_partythen=='PMDB')])))
elite.weight.ideol.sp<-data.frame((1/3)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_uf=='SP')])))
elite.weight.ideol.mg<-data.frame((1/3)/prop.table(table(elite.data$elite_year[!is.na(elite.data$elite_ideol)&(elite.data$elite_uf=='MG')])))

elite.data$weight.econ.all<-ifelse(is.na(elite.data$elite_econ),NA,elite.weight.econ.all[match(elite.data$elite_year, elite.weight.econ.all[,1]),2])
elite.data$weight.econ.evang<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_evang,NA,elite.weight.econ.evang[match(elite.data$elite_year, elite.weight.econ.evang[,1]),2])
elite.data$weight.econ.female<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_female,NA,elite.weight.econ.female[match(elite.data$elite_year, elite.weight.econ.female[,1]),2])
elite.data$weight.econ.black<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_black,NA,elite.weight.econ.black[match(elite.data$elite_year, elite.weight.econ.black[,1]),2])
elite.data$weight.econ.loweduc<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_loweduc,NA,elite.weight.econ.loweduc[match(elite.data$elite_year, elite.weight.econ.loweduc[,1]),2])
elite.data$weight.econ.pt<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_partythen=='PT',NA,elite.weight.econ.pt[match(elite.data$elite_year, elite.weight.econ.pt[,1]),2])
elite.data$weight.econ.psdb<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_partythen=='PSDB',NA,elite.weight.econ.psdb[match(elite.data$elite_year, elite.weight.econ.psdb[,1]),2])
elite.data$weight.econ.pmdb<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_partythen=='PMDB',NA,elite.weight.econ.pmdb[match(elite.data$elite_year, elite.weight.econ.pmdb[,1]),2])
elite.data$weight.econ.sp<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_uf=='SP',NA,elite.weight.econ.sp[match(elite.data$elite_year, elite.weight.econ.sp[,1]),2])
elite.data$weight.econ.mg<-ifelse(is.na(elite.data$elite_econ)|!elite.data$elite_uf=='MG',NA,elite.weight.econ.mg[match(elite.data$elite_year, elite.weight.econ.mg[,1]),2])
elite.data$weight.ideol.all<-ifelse(is.na(elite.data$elite_ideol),NA,elite.weight.ideol.all[match(elite.data$elite_year, elite.weight.ideol.all[,1]),2])
elite.data$weight.ideol.evang<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_evang,NA,elite.weight.ideol.evang[match(elite.data$elite_year, elite.weight.ideol.evang[,1]),2])
elite.data$weight.ideol.female<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_female,NA,elite.weight.ideol.female[match(elite.data$elite_year, elite.weight.ideol.female[,1]),2])
elite.data$weight.ideol.black<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_black,NA,elite.weight.ideol.black[match(elite.data$elite_year, elite.weight.ideol.black[,1]),2])
elite.data$weight.ideol.loweduc<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_loweduc,NA,elite.weight.ideol.loweduc[match(elite.data$elite_year, elite.weight.ideol.loweduc[,1]),2])
elite.data$weight.ideol.pt<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_partythen=='PT',NA,elite.weight.ideol.pt[match(elite.data$elite_year, elite.weight.ideol.pt[,1]),2])
elite.data$weight.ideol.psdb<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_partythen=='PSDB',NA,elite.weight.ideol.psdb[match(elite.data$elite_year, elite.weight.ideol.psdb[,1]),2])
elite.data$weight.ideol.pmdb<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_partythen=='PMDB',NA,elite.weight.ideol.pmdb[match(elite.data$elite_year, elite.weight.ideol.pmdb[,1]),2])
elite.data$weight.ideol.sp<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_uf=='SP',NA,elite.weight.ideol.sp[match(elite.data$elite_year, elite.weight.ideol.sp[,1]),2])
elite.data$weight.ideol.mg<-ifelse(is.na(elite.data$elite_ideol)|!elite.data$elite_uf=='MG',NA,elite.weight.ideol.mg[match(elite.data$elite_year, elite.weight.ideol.mg[,1]),2])

# KS statistics

ks.econ.all<-wtd.ks(elite.data$elite_econ,mass.data$mass_econ,elite.data$weight.econ.all,mass.data$mass_pweight*mass.data$weight.econ.all)
ks.econ.evang<-wtd.ks(elite.data$elite_econ[elite.data$elite_evang],mass.data$mass_econ[mass.data$mass_evang],elite.data$weight.econ.evang[elite.data$elite_evang],mass.data$mass_pweight[mass.data$mass_evang]*mass.data$weight.econ.evang[mass.data$mass_evang])
ks.econ.female<-wtd.ks(elite.data$elite_econ[elite.data$elite_female],mass.data$mass_econ[mass.data$mass_female],elite.data$weight.econ.female[elite.data$elite_female],mass.data$mass_pweight[mass.data$mass_female]*mass.data$weight.econ.female[mass.data$mass_female])
ks.econ.black<-wtd.ks(elite.data$elite_econ[elite.data$elite_black],mass.data$mass_econ[mass.data$mass_black],elite.data$weight.econ.black[elite.data$elite_black],mass.data$mass_pweight[mass.data$mass_black]*mass.data$weight.econ.black[mass.data$mass_black])
ks.econ.loweduc<-wtd.ks(elite.data$elite_econ[elite.data$elite_loweduc],mass.data$mass_econ[mass.data$mass_loweduc],elite.data$weight.econ.loweduc[elite.data$elite_loweduc],mass.data$mass_pweight[mass.data$mass_loweduc]*mass.data$weight.econ.loweduc[mass.data$mass_loweduc])
ks.econ.pt<-wtd.ks(elite.data$elite_econ[elite.data$elite_partythen=='PT'],mass.data$mass_econ[mass.data$mass_party=='PT'],elite.data$weight.econ.pt[elite.data$elite_partythen=='PT'],mass.data$mass_pweight[mass.data$mass_party=='PT']*mass.data$weight.econ.pt[mass.data$mass_party=='PT'])
ks.econ.psdb<-wtd.ks(elite.data$elite_econ[elite.data$elite_partythen=='PSDB'],mass.data$mass_econ[mass.data$mass_party=='PSDB'],elite.data$weight.econ.psdb[elite.data$elite_partythen=='PSDB'],mass.data$mass_pweight[mass.data$mass_party=='PSDB']*mass.data$weight.econ.psdb[mass.data$mass_party=='PSDB'])
ks.econ.pmdb<-wtd.ks(elite.data$elite_econ[elite.data$elite_partythen=='PMDB'],mass.data$mass_econ[mass.data$mass_party=='PMDB'],elite.data$weight.econ.pmdb[elite.data$elite_partythen=='PMDB'],mass.data$mass_pweight[mass.data$mass_party=='PMDB']*mass.data$weight.econ.pmdb[mass.data$mass_party=='PMDB'])
ks.econ.sp<-wtd.ks(elite.data$elite_econ[elite.data$elite_uf=='SP'],mass.data$mass_econ[mass.data$mass_uf=='SP'],elite.data$weight.econ.sp[elite.data$elite_uf=='SP'],mass.data$mass_pweight[mass.data$mass_uf=='SP']*mass.data$weight.econ.sp[mass.data$mass_uf=='SP'])
ks.econ.mg<-wtd.ks(elite.data$elite_econ[elite.data$elite_uf=='MG'],mass.data$mass_econ[mass.data$mass_uf=='MG'],elite.data$weight.econ.mg[elite.data$elite_uf=='MG'],mass.data$mass_pweight[mass.data$mass_uf=='MG']*mass.data$weight.econ.mg[mass.data$mass_uf=='MG'])

ks.ideol.all<-wtd.ks(elite.data$elite_ideol,mass.data$mass_ideol,elite.data$weight.ideol.all,mass.data$mass_pweight*mass.data$weight.ideol.all)
ks.ideol.evang<-wtd.ks(elite.data$elite_ideol[elite.data$elite_evang],mass.data$mass_ideol[mass.data$mass_evang],elite.data$weight.ideol.evang[elite.data$elite_evang],mass.data$mass_pweight[mass.data$mass_evang]*mass.data$weight.ideol.evang[mass.data$mass_evang])
ks.ideol.female<-wtd.ks(elite.data$elite_ideol[elite.data$elite_female],mass.data$mass_ideol[mass.data$mass_female],elite.data$weight.ideol.female[elite.data$elite_female],mass.data$mass_pweight[mass.data$mass_female]*mass.data$weight.ideol.female[mass.data$mass_female])
ks.ideol.black<-wtd.ks(elite.data$elite_ideol[elite.data$elite_black],mass.data$mass_ideol[mass.data$mass_black],elite.data$weight.ideol.black[elite.data$elite_black],mass.data$mass_pweight[mass.data$mass_black]*mass.data$weight.ideol.black[mass.data$mass_black])
ks.ideol.loweduc<-wtd.ks(elite.data$elite_ideol[elite.data$elite_loweduc],mass.data$mass_ideol[mass.data$mass_loweduc],elite.data$weight.ideol.loweduc[elite.data$elite_loweduc],mass.data$mass_pweight[mass.data$mass_loweduc]*mass.data$weight.ideol.loweduc[mass.data$mass_loweduc])
ks.ideol.pt<-wtd.ks(elite.data$elite_ideol[elite.data$elite_partythen=='PT'],mass.data$mass_ideol[mass.data$mass_party=='PT'],elite.data$weight.ideol.pt[elite.data$elite_partythen=='PT'],mass.data$mass_pweight[mass.data$mass_party=='PT']*mass.data$weight.ideol.pt[mass.data$mass_party=='PT'])
ks.ideol.psdb<-wtd.ks(elite.data$elite_ideol[elite.data$elite_partythen=='PSDB'],mass.data$mass_ideol[mass.data$mass_party=='PSDB'],elite.data$weight.ideol.psdb[elite.data$elite_partythen=='PSDB'],mass.data$mass_pweight[mass.data$mass_party=='PSDB']*mass.data$weight.ideol.psdb[mass.data$mass_party=='PSDB'])
ks.ideol.pmdb<-wtd.ks(elite.data$elite_ideol[elite.data$elite_partythen=='PMDB'],mass.data$mass_ideol[mass.data$mass_party=='PMDB'],elite.data$weight.ideol.pmdb[elite.data$elite_partythen=='PMDB'],mass.data$mass_pweight[mass.data$mass_party=='PMDB']*mass.data$weight.ideol.pmdb[mass.data$mass_party=='PMDB'])
ks.ideol.sp<-wtd.ks(elite.data$elite_ideol[elite.data$elite_uf=='SP'],mass.data$mass_ideol[mass.data$mass_uf=='SP'],elite.data$weight.ideol.sp[elite.data$elite_uf=='SP'],mass.data$mass_pweight[mass.data$mass_uf=='SP']*mass.data$weight.ideol.sp[mass.data$mass_uf=='SP'])
ks.ideol.mg<-wtd.ks(elite.data$elite_ideol[elite.data$elite_uf=='MG'],mass.data$mass_ideol[mass.data$mass_uf=='MG'],elite.data$weight.ideol.mg[elite.data$elite_uf=='MG'],mass.data$mass_pweight[mass.data$mass_uf=='MG']*mass.data$weight.ideol.mg[mass.data$mass_uf=='MG'])

ks.affirm.all<-wtd.ks(elite.data$elite_affirm,mass.data$mass_affirm, NULL,mass.data$mass_pweight)
ks.affirm.evang<-wtd.ks(elite.data$elite_affirm[elite.data$elite_evang],mass.data$mass_affirm[mass.data$mass_evang], NULL,mass.data$mass_pweight[mass.data$mass_evang])
ks.affirm.female<-wtd.ks(elite.data$elite_affirm[elite.data$elite_female],mass.data$mass_affirm[mass.data$mass_female], NULL,mass.data$mass_pweight[mass.data$mass_female])
ks.affirm.black<-wtd.ks(elite.data$elite_affirm[elite.data$elite_black],mass.data$mass_affirm[mass.data$mass_black], NULL,mass.data$mass_pweight[mass.data$mass_black])
ks.affirm.loweduc<-wtd.ks(elite.data$elite_affirm[elite.data$elite_loweduc],mass.data$mass_affirm[mass.data$mass_loweduc], NULL,mass.data$mass_pweight[mass.data$mass_loweduc])
ks.affirm.pt<-wtd.ks(elite.data$elite_affirm[elite.data$elite_partythen=='PT'],mass.data$mass_affirm[mass.data$mass_party=='PT'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PT'])
ks.affirm.psdb<-wtd.ks(elite.data$elite_affirm[elite.data$elite_partythen=='PSDB'],mass.data$mass_affirm[mass.data$mass_party=='PSDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PSDB'])
ks.affirm.pmdb<-wtd.ks(elite.data$elite_affirm[elite.data$elite_partythen=='PMDB'],mass.data$mass_affirm[mass.data$mass_party=='PMDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PMDB'])
ks.affirm.sp<-wtd.ks(elite.data$elite_affirm[elite.data$elite_uf=='SP'],mass.data$mass_affirm[mass.data$mass_uf=='SP'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='SP'])
ks.affirm.mg<-wtd.ks(elite.data$elite_affirm[elite.data$elite_uf=='MG'],mass.data$mass_affirm[mass.data$mass_uf=='MG'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='MG'])


ks.classaffirm.all<-wtd.ks(elite.data$elite_classaffirm,mass.data$mass_classaffirm, NULL,mass.data$mass_pweight)
ks.classaffirm.evang<-wtd.ks(elite.data$elite_classaffirm[elite.data$elite_evang],mass.data$mass_classaffirm[mass.data$mass_evang], NULL,mass.data$mass_pweight[mass.data$mass_evang])
ks.classaffirm.female<-wtd.ks(elite.data$elite_classaffirm[elite.data$elite_female],mass.data$mass_classaffirm[mass.data$mass_female], NULL,mass.data$mass_pweight[mass.data$mass_female])
ks.classaffirm.black<-wtd.ks(elite.data$elite_classaffirm[elite.data$elite_black],mass.data$mass_classaffirm[mass.data$mass_black], NULL,mass.data$mass_pweight[mass.data$mass_black])
ks.classaffirm.loweduc<-wtd.ks(elite.data$elite_classaffirm[elite.data$elite_loweduc],mass.data$mass_classaffirm[mass.data$mass_loweduc], NULL,mass.data$mass_pweight[mass.data$mass_loweduc])
ks.classaffirm.pt<-wtd.ks(elite.data$elite_classaffirm[elite.data$elite_partythen=='PT'],mass.data$mass_classaffirm[mass.data$mass_party=='PT'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PT'])
ks.classaffirm.psdb<-wtd.ks(elite.data$elite_classaffirm[elite.data$elite_partythen=='PSDB'],mass.data$mass_classaffirm[mass.data$mass_party=='PSDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PSDB'])
ks.classaffirm.pmdb<-wtd.ks(elite.data$elite_classaffirm[elite.data$elite_partythen=='PMDB'],mass.data$mass_classaffirm[mass.data$mass_party=='PMDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PMDB'])
ks.classaffirm.sp<-wtd.ks(elite.data$elite_classaffirm[elite.data$elite_uf=='SP'],mass.data$mass_classaffirm[mass.data$mass_uf=='SP'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='SP'])
ks.classaffirm.mg<-wtd.ks(elite.data$elite_classaffirm[elite.data$elite_uf=='MG'],mass.data$mass_classaffirm[mass.data$mass_uf=='MG'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='MG'])

ks.abortion.all<-wtd.ks(elite.data$elite_abortion,mass.data$mass_abortion, NULL,mass.data$mass_pweight)
ks.abortion.evang<-wtd.ks(elite.data$elite_abortion[elite.data$elite_evang],mass.data$mass_abortion[mass.data$mass_evang], NULL,mass.data$mass_pweight[mass.data$mass_evang])
ks.abortion.female<-wtd.ks(elite.data$elite_abortion[elite.data$elite_female],mass.data$mass_abortion[mass.data$mass_female], NULL,mass.data$mass_pweight[mass.data$mass_female])
ks.abortion.black<-wtd.ks(elite.data$elite_abortion[elite.data$elite_black],mass.data$mass_abortion[mass.data$mass_black], NULL,mass.data$mass_pweight[mass.data$mass_black])
ks.abortion.loweduc<-wtd.ks(elite.data$elite_abortion[elite.data$elite_loweduc],mass.data$mass_abortion[mass.data$mass_loweduc], NULL,mass.data$mass_pweight[mass.data$mass_loweduc])
ks.abortion.pt<-wtd.ks(elite.data$elite_abortion[elite.data$elite_partythen=='PT'],mass.data$mass_abortion[mass.data$mass_party=='PT'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PT'])
ks.abortion.psdb<-wtd.ks(elite.data$elite_abortion[elite.data$elite_partythen=='PSDB'],mass.data$mass_abortion[mass.data$mass_party=='PSDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PSDB'])
ks.abortion.pmdb<-wtd.ks(elite.data$elite_abortion[elite.data$elite_partythen=='PMDB'],mass.data$mass_abortion[mass.data$mass_party=='PMDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PMDB'])
ks.abortion.sp<-wtd.ks(elite.data$elite_abortion[elite.data$elite_uf=='SP'],mass.data$mass_abortion[mass.data$mass_uf=='SP'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='SP'])
ks.abortion.mg<-wtd.ks(elite.data$elite_abortion[elite.data$elite_uf=='MG'],mass.data$mass_abortion[mass.data$mass_uf=='MG'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='MG'])

ks.gaymarriage.all<-wtd.ks(elite.data$elite_gaymarriage,mass.data$mass_gaymarriage, NULL,mass.data$mass_pweight)
ks.gaymarriage.evang<-wtd.ks(elite.data$elite_gaymarriage[elite.data$elite_evang],mass.data$mass_gaymarriage[mass.data$mass_evang], NULL,mass.data$mass_pweight[mass.data$mass_evang])
ks.gaymarriage.female<-wtd.ks(elite.data$elite_gaymarriage[elite.data$elite_female],mass.data$mass_gaymarriage[mass.data$mass_female], NULL,mass.data$mass_pweight[mass.data$mass_female])
ks.gaymarriage.black<-wtd.ks(elite.data$elite_gaymarriage[elite.data$elite_black],mass.data$mass_gaymarriage[mass.data$mass_black], NULL,mass.data$mass_pweight[mass.data$mass_black])
ks.gaymarriage.loweduc<-wtd.ks(elite.data$elite_gaymarriage[elite.data$elite_loweduc],mass.data$mass_gaymarriage[mass.data$mass_loweduc], NULL,mass.data$mass_pweight[mass.data$mass_loweduc])
ks.gaymarriage.pt<-wtd.ks(elite.data$elite_gaymarriage[elite.data$elite_partythen=='PT'],mass.data$mass_gaymarriage[mass.data$mass_party=='PT'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PT'])
ks.gaymarriage.psdb<-wtd.ks(elite.data$elite_gaymarriage[elite.data$elite_partythen=='PSDB'],mass.data$mass_gaymarriage[mass.data$mass_party=='PSDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PSDB'])
ks.gaymarriage.pmdb<-wtd.ks(elite.data$elite_gaymarriage[elite.data$elite_partythen=='PMDB'],mass.data$mass_gaymarriage[mass.data$mass_party=='PMDB'], NULL,mass.data$mass_pweight[mass.data$mass_party=='PMDB'])
ks.gaymarriage.sp<-wtd.ks(elite.data$elite_gaymarriage[elite.data$elite_uf=='SP'],mass.data$mass_gaymarriage[mass.data$mass_uf=='SP'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='SP'])
ks.gaymarriage.mg<-wtd.ks(elite.data$elite_gaymarriage[elite.data$elite_uf=='MG'],mass.data$mass_gaymarriage[mass.data$mass_uf=='MG'], NULL,mass.data$mass_pweight[mass.data$mass_uf=='MG'])

# Environment: unweighted because not drawing from BEPS or AB 2012.

ks.environment.all<-ks(elite.data$elite_environment,mass.data$mass_environment)
ks.environment.evang<-ks(elite.data$elite_environment[elite.data$elite_evang],mass.data$mass_environment[mass.data$mass_evang])
ks.environment.female<-ks(elite.data$elite_environment[elite.data$elite_female],mass.data$mass_environment[mass.data$mass_female])
ks.environment.black<-ks(elite.data$elite_environment[elite.data$elite_black],mass.data$mass_environment[mass.data$mass_black])
ks.environment.loweduc<-ks(elite.data$elite_environment[elite.data$elite_loweduc],mass.data$mass_environment[mass.data$mass_loweduc])
ks.environment.pt<-ks(elite.data$elite_environment[elite.data$elite_partythen=='PT'],mass.data$mass_environment[mass.data$mass_party=='PT'])
ks.environment.psdb<-ks(elite.data$elite_environment[elite.data$elite_partythen=='PSDB'],mass.data$mass_environment[mass.data$mass_party=='PSDB'])
ks.environment.pmdb<-ks(elite.data$elite_environment[elite.data$elite_partythen=='PMDB'],mass.data$mass_environment[mass.data$mass_party=='PMDB'])
ks.environment.sp<-ks(elite.data$elite_environment[elite.data$elite_uf=='SP'],mass.data$mass_environment[mass.data$mass_uf=='SP'])
ks.environment.mg<-ks(elite.data$elite_environment[elite.data$elite_uf=='MG'],mass.data$mass_environment[mass.data$mass_uf=='MG'])

ks.table <-matrix(c(
	ks.econ.all, ks.econ.evang, ks.econ.female, ks.econ.black, ks.econ.loweduc, ks.econ.pt, ks.econ.psdb, ks.econ.pmdb, ks.econ.sp, ks.econ.mg,
	ks.ideol.all, ks.ideol.evang, ks.ideol.female, ks.ideol.black, ks.ideol.loweduc, ks.ideol.pt, ks.ideol.psdb, ks.ideol.pmdb, ks.ideol.sp, ks.ideol.mg,
	ks.affirm.all, ks.affirm.evang, ks.affirm.female, ks.affirm.black, ks.affirm.loweduc, ks.affirm.pt, ks.affirm.psdb, ks.affirm.pmdb, ks.affirm.sp, ks.affirm.mg,
	ks.classaffirm.all, ks.classaffirm.evang, ks.classaffirm.female, ks.classaffirm.black, ks.classaffirm.loweduc, ks.classaffirm.pt, ks.classaffirm.psdb, ks.classaffirm.pmdb, ks.classaffirm.sp, ks.classaffirm.mg,
	ks.abortion.all, ks.abortion.evang, ks.abortion.female, ks.abortion.black, ks.abortion.loweduc, ks.abortion.pt, ks.abortion.psdb, ks.abortion.pmdb, ks.abortion.sp, ks.abortion.mg,
	ks.gaymarriage.all, ks.gaymarriage.evang, ks.gaymarriage.female, ks.gaymarriage.black, ks.gaymarriage.loweduc, ks.gaymarriage.pt, ks.gaymarriage.psdb, ks.gaymarriage.pmdb, ks.gaymarriage.sp, ks.gaymarriage.mg,
	ks.environment.all, ks.environment.evang, ks.environment.female, ks.environment.black, ks.environment.loweduc, ks.environment.pt, ks.environment.psdb, ks.environment.pmdb, ks.environment.sp, ks.environment.mg),ncol=10,byrow=T)
ks.table <-round(ks.table,3)
rownames(ks.table)<-c('Economic\nRegime\n(2002-13)','Ideology\n(2002-13)','Race\nQuotas\n(2010-13)','Class\nQuotas\n(2010-13)','Abortion\n(2010-13)','Gay\nMarriage\n(2010-13)','Environment\n(2011-13)')
colnames(ks.table)<-c('All','Evangelicals','Women','Afro-Brazilians','Low Education','PT','PSDB','PMDB','SP','MG')

# Plot
y<-matrix(7:1*2,ncol=10,nrow=7) + matrix(seq(-.9,.9,.2)*.75,ncol=10,nrow=7,byrow=T)

pdf(file='wtd_ks_points_plot.pdf',height=10,width=7.5)
par(mar=c(5,6,2,2)+.1)
plot(ks.table,y,pch=matrix(c(8,21:24,21:23,21:22),nrow=7,ncol=10,byrow=T),bg=matrix(c(NA,rep('black',4),rep('grey',3),rep('white',2)),nrow=7,ncol=10,byrow=T),xlab='KS Statistic',yaxt='n',ylab='',xlim=c(0,.9),ylim=range(y))
abline(h=(9:2)*2-1,lty=3,lwd=1)
legend('topright',legend=c('All','Evangelical','Female','Afro-Brazilian','No College','PT','PSDB','PMDB','SP','MG'),pch=c(8,21:24,21:23,21:22),pt.bg=c(NA,rep('black',4),rep('grey',3),rep('white',2)),bg='white')
Axis(side=2,at=(7:1)*2,labels=rownames(ks.table),las=2,hadj=0,tick=F,line=4)
dev.off()
